package com.example.administrator.zyfypt_211dsh.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.administrator.zyfypt_211dsh.R;
import com.example.administrator.zyfypt_211dsh.bean.LoginBean;
import com.example.administrator.zyfypt_211dsh.bean.SessionID;
import com.example.administrator.zyfypt_211dsh.iface.LoginListener;
import com.example.administrator.zyfypt_211dsh.model.LoginModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btn,btn1,btn2;
    private EditText etname,etpass;
    private Switch sw;
    private String username="",password="";//用户名，密码
    private SharedPreferences sp;//简单存储对象

    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            SessionID.sessionID = loginBean.getSessionid();
            SessionID.username = loginBean.getUsername();
            System.out.println("result:"+loginBean.getId());
            if(loginBean.getId()!=null) {
                Intent intent1 = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent1);
            }
        }

        @Override
        public void onFail(String msg) {
            System.out.println("fail");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toast.makeText(this, "登录", Toast.LENGTH_SHORT).show();
        init();

        //获取存储信息
        sp=getSharedPreferences("Login",MODE_PRIVATE);
        //读取存储信息
        readSP();
    }

    private void init() {
        btn=(Button)findViewById(R.id.button);
        btn1=(Button)findViewById(R.id.button2);
        btn2=(Button)findViewById(R.id.button_for);
        etname=(EditText)findViewById(R.id.editText);
        etpass=(EditText)findViewById(R.id.editText2);
        sw=(Switch)findViewById(R.id.switch1);


        btn.setOnClickListener(this);
        btn1.setOnClickListener(this);
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(sw.isChecked()){
                    saveSP();
                }
                else{
                    etname.setText("");
                    etpass.setText("");
                    saveSP();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                if(etname.getText().toString().equals("") && etpass.getText().toString().equals("")){
                 //   Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
                    break;
                }
                //实例化LoginModel
                LoginModel loginModel = new LoginModel();
                //请求UserInfo
                loginModel.getResult(etname.getText().toString(),etpass.getText().toString(), loginListener);
                //然后这里转onResponse方法处理响应回来的数据
//                Intent intent = new Intent(activity_login.this, activity_main.class);
//                intent.putExtra("param1","hello,  ");
//                intent.putExtra("param2",5);
//                startActivity(intent);
//                username=etname.getText().toString();
//                password=etpass.getText().toString();
//                LoginModel loginModel=new LoginModel();
//                loginModel.getResult(username,password,loginListener);
//                if(username.equals("QQQQ1") && password.equals("asd"))
//                {
//                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//                    if(sw.isChecked())
//                    {
//                        saveSP();//存储信息
//                    }
//                }
//                System.out.println("点击登录按钮！");
////                LoginModel loginModel = new LoginModel();
////                loginModel.getResult(idET.getText().toString(),
////                        pwdEt.getText().toString(),
////                        loginListener);
//                Intent intent1 = new Intent();
//                intent1.setClass(this, MainActivity.class);
//                startActivity(intent1);
                break;
            case R.id.button2:
                System.out.println("点击注册按钮！");
                Intent intent = new Intent();
                intent.setClass(this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.button_for:
                System.out.println("点击忘记按钮！");
                Intent intent2 = new Intent();
                intent2.setClass(this, ForgotActivity.class);
                startActivity(intent2);
                break;

        }
    }
    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();//提交存储
    }
    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        Boolean b=sp.getBoolean("remember",false);
        if(b)
        {
            etname.setText(name);
            etpass.setText(pass);
            sw.setChecked(b);
        }

    }
}
